var options = {
	locale: "cn",
	scaling: 1,
	nodes: {
		shape: 'circle',
		margin: 10,
		widthConstraint: {
			maximum: 55
		},
	},
	//	interaction: {
	//		zoomView: true
	//	},
	edges: {
		//		smooth: true,
		widthConstraint: {
			maximum: 90
		},
		length: 80,
		font: {
			size: 12,
			align: 'top'
		},
		arrows: {
			to: true
		}
	},
	interaction: {
		dragNodes: false,
	},

	layout: {
		randomSeed: 1,
		improvedLayout: true,
		hierarchical: {
			enabled: true,
			levelSeparation: 120,
			nodeSpacing: 150,
			treeSpacing: 200,
			blockShifting: true,
			edgeMinimization: true,
			parentCentralization: true,
			direction: 'UD', // UD, DU, LR, RL
			sortMethod: 'hubsize' // hubsize, directed
		}
	}
}
var nodes = [];
// create some edges
var edges = [];
// create a network
var container = document.getElementById('mynetwork');
var data = {
	nodes: nodes,
	edges: edges
};

window.onload = function() {
	var thisId = GetUrlByParamName("occuoationId")
	if(thisId != null) {
		getNodesAndEdges(thisId)
	} else {
		alert(thisId)
	}
}

function GetUrlByParamName(name)   {    
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");    
	var URL =  decodeURI(window.location.search);    
	var r = URL.substr(1).match(reg);    
	if(r != null) {       //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
		return  decodeURI(r[2]);    
	};    
	return null;  
}

function getNodesAndEdges(occuoationId) {
	$.ajax({
		type: "get",
		url: "../../zte-api/api/neo/course/findCourseByoccuoationId?id=" + occuoationId,
		contentType: "application/json",
		success: function(r) {
			nodes = r.data.nodes;
			// create some edges
			edges = r.data.edges;
			//create a network
			data = {
				nodes: nodes,
				edges: edges
			};
			var network = new vis.Network(container, data, options);
			network.on("click", function(params) {
				//if (/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent)) 
				if(/(Android)/i.test(navigator.userAgent)) {
					window.android.showPopWindow(params.nodes[0])
				} else {
					window.webkit.messageHandlers.showPopWindow.postMessage(params.nodes[0])
				}
			});
			network.on("stabilizationProgress", function(params) {
				var maxWidth = 496;
				var minWidth = 20;
				var widthFactor = params.iterations / params.total;
				var width = Math.max(minWidth, maxWidth * widthFactor);
				document.getElementById('bar').style.width = width + 'px';
				document.getElementById('text').innerHTML = Math.round(widthFactor * 100) + '%';
			});
			network.once("stabilizationIterationsDone", function() {
				document.getElementById('text').innerHTML = '100%';
				document.getElementById('bar').style.width = '496px';
				document.getElementById('loadingBar').style.opacity = 0;
				// really clean the dom element
				setTimeout(function() {
					document.getElementById('loadingBar').style.display = 'none';
				}, 500);
			});
		},
		error: function() {
			alert("error");
		}
	});
}